import $http from '@/api'
import config from '@/config'
import * as types from '../mutation-types'
const state = {
  normalCart: [],
  specCart: [], // 礼品购物车只能产生1个
  isHideCart: false,
  checkoutStatus: null,
  addressList: [],
  addressArea: [],
  free: null
}

// getters
const getters = {
  checkoutStatus: state => state.checkoutStatus,
  isHideCart: state => state.isHideCart,
  getAddressList: state => state.addressList,
  addressArea: state => state.addressArea,
  free: state => state.free
}

// actions
const actions = {
  clearCart ({ commit }, data) {
    commit(types.CLEAR_CART, data)
  },
  setCart({ commit }, type) {
    $http.getCartList(type)
    .then(res => {
      if (res.code === 0) {
        // 添加产品到购物车
        commit(types.SET_CART, {productData: res.data, type: type})
      }
      return res.data
    })
  },
  selectPackage({ commit }, data) {
    commit(types.SELECT_PACKAGE, data)
  },
  setAddressList({commit}) {
    $http.getAddressList()
    .then(res => {
      if (res.code === 0) {
        // 添加产品到购物车
        commit(types.SET_ADDRESS_LIST, res.data)
      }
      return res.data
    })
  },
  getAddressArea({ commit }) {
    $http.getAddressArea()
    .then(res => {
      if (res.code === 0) {
        commit(types.GET_ADDRESS_AREA, res.data)
      }
    })
  }
}

// mutations
const mutations = {
  // 添加购物车
  [types.ADD_TO_CART] (state, { productData, type }) {
    // state.lastCheckout = null
    let productId = productData.product_id
    let cartProduct = state[config.cart[type]]
    // 获得购物车记录
    let record = cartProduct.find(item => {
        return item.product_id === productId
      })
    // 对购物车进行添加操作
    let quantity = 1
    if (!record) {
      let packageData = productData.package // 默认礼包数据
      let tmpData = {
        product_id: productId,
        quantity: quantity,
        price: productData.price,
        product_name: productData.product_name,
        spec: packageData || [],
        spec_name: (packageData && packageData[0] ? packageData[0].spec_name : ''), // 默认为第一个产品
        spec_image: productData.list_images || ''
      }
      cartProduct.push(tmpData)
      record = tmpData
    } else {
      record.quantity++
      quantity = record.quantity
    }
    // 向服务器提交购物车添加
    let params = {product_id: productId, cart_type: type, quantity: quantity}
    if (type === 2) {
      params.spec_name = record.spec_name
    }
    $http.addCart(params)
      .then(res => {
        if (res.code === 0) {
          console.log('添加成功车成功')
        }
      })
  },
  [types.DECREASE_CART] (state, { productData, type }) {
    let index = 0
    const productId = productData.product_id
    let cartProduct = state[config.cart[type]]
    // 获得购物车记录
    let quantity = 1
    let record = cartProduct.find((item, i) => {
        index = i
        return item.product_id === productId
      })
    if (record.quantity <= 1) {
      cartProduct.splice(index, 1)
      if (!cartProduct.length) {
        state.isHideCart = false
      }
    }
    if (record.quantity === 0) {
      return
    }
    record.quantity--
    quantity = record.quantity
    // 向服务器提交购物车添加
    let params = {product_id: productId, cart_type: type, quantity: quantity}
    if (type === 2) {
      params.spec_name = record.spec_name
    }
    $http.addCart(params)
      .then(res => {
        if (res.code === 0) {
          console.log('减少购物车成功')
        }
      })
  },
  [types.SET_CART] (state, data) {
    state[config.cart[data.type]] = data.productData
  },
  [types.CLEAR_CART] (state, {type}) {
    state[config.cart[type]] = []
    state.isHideCart = false
    // 向服务器提交清空操作
    $http.cartClear(type)
      .then(res => {
        if (res.code === 0) {
          console.log('清空了购物车')
        }
      })
  },
  [types.GET_FREE] (state, data) {
    state.free = data
  },
  [types.SELECT_PACKAGE] (state, data) {
    let type = 2
    let specCart = state[config.cart[type]] // 礼品购物车只有一个
    let productId = data.product_id
    let record = specCart.find(item => {
      return item.product_id === productId
    })
    let quantity = 1
    if (!record) {
      data.quantity = 1
      specCart.push(data)
      record = data
    } else {
      record.price = data.price
      record.spec_name = data.spec_name
      record.spec_image = data.spec_image || record.spec_image
    }
    quantity = record.quantity
    // 向服务器提交购物车添加
    let params = {product_id: productId, cart_type: type, quantity: quantity}
    if (type === 2) {
      params.spec_name = record.spec_name
    }
    $http.addCart(params)
      .then(res => {
        if (res.code === 0) {
          console.log('更新用户切换时购物车')
        }
      })
  },
  [types.HIDE_CART_LIST] (state) {
    state.isHideCart = !state.isHideCart
  },
  [types.SET_ADDRESS_LIST] (state, data) {
    state.addressList = data
  },
  [types.GET_ADDRESS_AREA] (state, data) {
    state.addressArea = data
  },
  [types.CHECKOUT_REQUEST] (state, {type}) {
    // clear cart
    state[config.cart[type]] = []
    state.checkoutStatus = null
  },

  [types.CHECKOUT_SUCCESS] (state) {
    state.checkoutStatus = 'successful'
  },

  [types.CHECKOUT_FAILURE] (state, { savedCartItems, type }) {
    // 在发送请求之前保存到回滚的购物车
    state[config.cart[type]] = savedCartItems
    state.checkoutStatus = 'failed'
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
